package com.longrise.android.byjk.plugins.productfeerate;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.longrise.android.byjk.R;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/6/2.
 * 选择图片的适配器
 */

public class ProductfeeGridAdapter extends BaseAdapter {

    private static final String TAG = "ProductfeeGridAdapter";
    private final Context mContext;
    private List<String> mList = new ArrayList<>();
    private OnItemDeleateListener mListener;

    public ProductfeeGridAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<String> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataSetInvalidated();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View rootView;
        if (convertView == null) {
            rootView = View.inflate(mContext, R.layout.item_productfee_gv, null);
        } else {
            rootView = convertView;
        }
        ImageView iv = (ImageView) rootView.findViewById(R.id.item_productfee_iv);
        ViewGroup.LayoutParams layoutParams = iv.getLayoutParams();
        int size = (AppUtil.getScreenWidth() - AppUtil.dip2px(60)) / 3;
        layoutParams.height = size;
        iv.setLayoutParams(layoutParams);
        Glide.with(mContext).load("file://" + mList.get(position)).into(iv);

        //删除按钮
        ImageView iv_delete = (ImageView) rootView.findViewById(R.id.item_productfee_delete);
        iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                   mListener.onDelete(mList.get(position));
                }
            }
        });

        return rootView;
    }

    /**
     * 设置每张图片的删除监听
     */
    public void setOnItemDeleateListener(OnItemDeleateListener listener) {
        this.mListener = listener;
    }

    interface OnItemDeleateListener {
        void onDelete(String path);
    }
}
